package com.typy.www.tpay.ui.acitivity;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.github.mikephil.charting.charts.LineChart;
import com.typy.www.tpay.R;
import com.typy.www.tpay.base.BaseBackActivity;
import com.typy.www.tpay.bean.bean.BaseReturn;
import com.typy.www.tpay.bean.bean.HomeBtnBean;
import com.typy.www.tpay.bean.listvm.ActivicationListVm;
import com.typy.www.tpay.bean.vm.ActivicationVm;
import com.typy.www.tpay.bean.vm.BuyVm;
import com.typy.www.tpay.bean.vm.UserInfoVm;
import com.typy.www.tpay.bean.vm.ZBaoAccountVm;
import com.typy.www.tpay.constance.Constance;
import com.typy.www.tpay.ui.adapter.IcotypeAdapter;
import com.typy.www.tpay.ui.adapter.RecordAdapter;
import com.typy.www.tpay.ui.prt.PtrListContent;
import com.typy.www.tpay.util.ClassHoldUtil;
import com.typy.www.tpay.util.GsonUtils;
import com.typy.www.tpay.util.Logutil;
import com.typy.www.tpay.util.ParamsUtil;
import com.typy.www.tpay.util.StringUtil;
import com.typy.www.tpay.util.UiHelper;
import com.typy.www.tpay.util.UpdataUiUtil;
import com.typy.www.tpay.util.listviewanimations.IntentUtil;
import com.typy.www.tpay.volley.HTTPUtils;
import com.typy.www.tpay.volley.VolleyListener;
import com.zhy.android.percent.support.PercentLinearLayout;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 13799 on 2018/7/19.
 */

public class ZBaoActivity extends BaseBackActivity  {


    public static void show(Context context) {
        IntentUtil.intent2Activity(context, ZBaoActivity.class);
    }

    private RecordAdapter adapter;

    @BindView(R.id.ll_root)
    LinearLayout llRoot;
    private PtrListContent ptrListContent;
    private ListView mListView;
    private ViewHolder holder;
    private ActivicationListVm mListVm;
    private List<ActivicationVm> mData = new ArrayList<>();
    private String zbaoMarkUrl="";
    private double keyong;
    private TextView shuLiangTitle;

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);
        addListView();
        initAdapter();
        updateView();
        refresh();
    }


    private void refresh() {
        getUserData();
        getAmountlistDate(1,true);
        getZpayremarkData();
    }

    @Override
    protected int getTitleBarResId() {
        return -1;
    }

    private void addListView() {
        ptrListContent = new PtrListContent(getActivity()) {
            @Override
            protected void getPage(int pageIndex, boolean refresh) {
                refresh();
            }
        };
        mListView = ptrListContent.getListView();
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,1);
        llRoot.addView(ptrListContent.getmView(), params);
        View view1 = LayoutInflater.from(getActivity()).inflate(R.layout.activity_zbao, null);
        holder = new ViewHolder(view1);
        mListView.addHeaderView(view1);
        View head = LayoutInflater.from(getActivity()).inflate(R.layout.head_record, null);
        mListView.addHeaderView(head);
        shuLiangTitle=head.findViewById(R.id.tv_amount);
        ptrListContent.hideHint();
        View footView = LayoutInflater.from(getActivity()).inflate(R.layout.foot_zbao, null);
        footView.findViewById(R.id.tv_sell).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ActivicationActivity.show(getActivity(),ClassHoldUtil.ZBAOCENTER,keyong);
            }
        });
        llRoot.addView(footView);

    }

    private void initAdapter() {
        adapter = new RecordAdapter(getActivity());
        adapter.setClassType(ClassHoldUtil.ZBAOCENTER);
        mListView.setAdapter(adapter);
    }


    private void updateView() {
        UpdataUiUtil.getDefaul().setText("Z宝数字中心", holder.tvTitle)
                .setText("Z宝总资产", holder.tvYueTitles)
                .setText("锁定资产", holder.tvFenxiangfenTitles)
                .setText("可用资产", holder.tvJifenTitles)
                .setText("Z宝说明", holder.btnTitlebarRight);

        holder.btnTitlebarLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        holder.btnTitlebarRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!StringUtil.isEmpty(zbaoMarkUrl))
                WebActivity.show(getActivity(), "Z宝说明", zbaoMarkUrl);
            }
        });
        holder.rgRoot.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb1:
                        holder.rb1.setTextColor(getActivity().getResources().getColor(R.color.white));
                        holder.rb2.setTextColor(getActivity().getResources().getColor(R.color.text_darkcolor));
                        holder.rb3.setTextColor(getActivity().getResources().getColor(R.color.text_darkcolor));
                        shuLiangTitle.setText("数额");
                        adapter.setClassType(ClassHoldUtil.ZBAOCENTER);
                        break;
                    case R.id.rb2:
                        holder.rb1.setTextColor(getActivity().getResources().getColor(R.color.text_darkcolor));
                        holder.rb2.setTextColor(getActivity().getResources().getColor(R.color.white));
                        holder.rb3.setTextColor(getActivity().getResources().getColor(R.color.text_darkcolor));
                        shuLiangTitle.setText("余额");
                        adapter.setClassType(ClassHoldUtil.ZBAOSHOUYI);
                        break;
                    case R.id.rb3:
                        holder.rb1.setTextColor(getActivity().getResources().getColor(R.color.text_darkcolor));
                        holder.rb2.setTextColor(getActivity().getResources().getColor(R.color.text_darkcolor));
                        holder.rb3.setTextColor(getActivity().getResources().getColor(R.color.white));
                        shuLiangTitle.setText("数额");
                        adapter.setClassType(ClassHoldUtil.ZBAOCENTER);
                        break;
                }
                getAmountlistDate(1,true);
            }
        });
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_linear;
    }

    @Override
    protected void onResume() {
        super.onResume();
        refresh();
    }

    //获取账号信息
    private void getUserData() {
        ParamsUtil params = new ParamsUtil();
        HTTPUtils.post(this, Constance.getZbaoaccountURL(), params.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                ZBaoAccountVm vm = GsonUtils.parseJSON(ret.getData().toString(), ZBaoAccountVm.class);
                if (vm != null) {
                    UpdataUiUtil.getDefaul().setText(vm.getFdYesterdayAmount(), holder.tvZuorishouyiDetail)
                            .setText(vm.getFdZbTotal(), holder.tvYue)
                            .setText(vm.getFdZbFrozen(), holder.tvFenxiangfen)
                            .setText(vm.getFdZbCanUse(), holder.tvJifen);
                    keyong=vm.getFdZbCanUse();
                }

            }

            @Override
            public void onResponseErr(int code, String msg) {
                UiHelper.showToast(getActivity(), msg);
            }
        });
    }//获取z宝说明
    private void getZpayremarkData() {
        ParamsUtil params = new ParamsUtil();
        HTTPUtils.post(this, Constance.getZpayremarkURL(), params.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                ActivicationListVm   vm = GsonUtils.parseJSON(ret.getData().toString(), ActivicationListVm.class);
                if(vm!=null&&vm.getData().size()>0)
                    zbaoMarkUrl=vm.getData().get(0).getFdUrl();
            }

            @Override
            public void onResponseErr(int code, String msg) {
                UiHelper.showToast(getActivity(), msg);
            }
        });
    }

    //记录
    private void getAmountlistDate(final int page, final boolean refresh) {
        ParamsUtil params = new ParamsUtil();
        params.put("pageNum", page);
        params.put("pageSize", Constance.LIMT);
        String url="";
        if(holder.rb1.isChecked()){
            url=Constance.getZbaoturninlistURL();
        }else if(holder.rb2.isChecked()){
            url=Constance.getZbaoincomelistURL();
        }else {
            url=Constance.getZbaoturnoutlistURL();
        }

        HTTPUtils.post(getActivity(), url, params.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                if (ret.getCode() == 200 && ret.getData() != null) {
                    if (refresh || page == 0) {
                        mData.clear();
                        adapter.notifyDataSetChanged();
                    }
                    mListVm = GsonUtils.parseJSON(ret.getData().toString(), ActivicationListVm.class);
//                    mListVm = GsonUtils.parseJSON(test, ActivicationListVm.class);
                    JSONObject object;
                    try {
                        object=new JSONObject(ret.getData().toString());
                        JSONArray array=object.getJSONArray("data");
                        for (int i = 0; i< array.length(); i++) {
                         JSONObject object1=  array.getJSONObject(i);
                           Logutil.print("ammo=="+object1.getString("fdAmount"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    for(ActivicationVm vm:mListVm.getData()){
                        Logutil.print("vm=="+vm.getFdAmount());
                    }
                    if (mListVm != null && mListVm.getData() != null && mListVm.getData().size() != 0) {
                        mData.addAll(mListVm.getData());
                        adapter.setData(mData);
                        adapter.notifyDataSetChanged();
                    } else {
                        UiHelper.showToast(getActivity(), "暂无数据");
                    }
                }
                ptrListContent.loadInitialPage(mListVm.getCount());
            }

            @Override
            public void onResponseErr(int code, String msg) {
                UiHelper.showToast(getActivity(), msg);
            }
        });
    }
    static class ViewHolder {
        @BindView(R.id.btn_titlebar_left)
        ImageButton btnTitlebarLeft;
        @BindView(R.id.btn_titlebar_right)
        Button btnTitlebarRight;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_zuorishouyi_title)
        TextView tvZuorishouyiTitle;
        @BindView(R.id.tv_zuorishouyi_detail)
        TextView tvZuorishouyiDetail;
        @BindView(R.id.tv_yue_titles)
        TextView tvYueTitles;
        @BindView(R.id.tv_yue)
        TextView tvYue;
        @BindView(R.id.ll_amount)
        PercentLinearLayout llAmount;
        @BindView(R.id.tv_fenxiangfen_titles)
        TextView tvFenxiangfenTitles;
        @BindView(R.id.tv_fenxiangfen)
        TextView tvFenxiangfen;
        @BindView(R.id.ll_share)
        PercentLinearLayout llShare;
        @BindView(R.id.tv_jifen_titles)
        TextView tvJifenTitles;
        @BindView(R.id.tv_jifen)
        TextView tvJifen;
        @BindView(R.id.ll_point)
        PercentLinearLayout llPoint;
        @BindView(R.id.rb1)
        RadioButton rb1;
        @BindView(R.id.rb2)
        RadioButton rb2;
        @BindView(R.id.rb3)
        RadioButton rb3;
        @BindView(R.id.rg_root)
        RadioGroup rgRoot;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
